package com.soft.javase.day11.serial;

import java.io.*;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/9
 */
public class SerialDemo1 {

    public static void main(String[] args) {
        serial();
        unSerial();
    }

    public static void serial(){
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
        ){
            Person person = new Person("张三",18,"男");
            person.setHash();
            oos.writeObject(person);
            oos.flush();
            System.out.println("序列化成功");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void unSerial(){
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"));
        ){
            Person person = (Person) ois.readObject();
            System.out.println(person);
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
